package com.bbva.kyfb.kyfbbofo.implement;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bbva.jee.arq.spring.core.log.I18nLog;
import com.bbva.jee.arq.spring.core.log.I18nLogFactory;
import com.bbva.kyfb.kyfbdtofo.FicheroRequest;
import com.bbva.kyfb.kyfbdtofo.trf.DetalleTraspaso;
import com.bbva.kyfb.kyfbjpatlsb.dominiotlsb.Ttltrtra;
import com.bbva.kyfb.kyfbofo.abstracts.ExecuteTransferBOAbstract;
import com.bbva.kyfb.kyfbofo.interfaces.ExecuteTransactionBO;
import com.bbva.kyfb.kyfbutils.constantes.ConstantesFO;

@Service
public class TraspasoBO extends ExecuteTransferBOAbstract implements ExecuteTransactionBO {
	
	private static final I18nLog Log = I18nLogFactory.getLogI18n(TraspasoBO.class);

	@Override
	@Transactional("transactionManagerTLSB")
	protected void beforeTransaction(Object object, FicheroRequest aFicheroRequest) {		
		DetalleTraspaso detalleTraspaso = (DetalleTraspaso) object;	
		try {
			// int iLongRefFirma = 9;
			desTransfer = getTlsbGeneradorId().generarConLogon(9, aFicheroRequest.getReferencia());
			Ttltrtra tra = traDTO.buildTraEntityForTransferencia(detalleTraspaso, aFicheroRequest, desTransfer);
			getTraDao().insert(tra);
			getTraDao().flush();
		} catch (Exception e) {
			Log.error("Error realizando insert TLTRTRA", e);
		}
	}
	
	@Override
	public void launchTransaction(Object object, FicheroRequest ficheroRequest) {
		DetalleTraspaso detalleTraspaso = (DetalleTraspaso) object;		
		trfOutDto = accesoHost.relizaTransferenciaNacional(detalleTraspaso, ficheroRequest);
	}

	@Override
	@Transactional("transactionManagerTLSB")
	protected void afterTransaction(Object object) {
		String codError = trfOutDto.getCodError();		
		try {
			Ttltrtra tra = traDTO
					.buildTraUpdateEntityForTransferencia(desTransfer,trfOutDto);
			getTraDao().update(tra);
			getTraDao().flush();
		} catch (Exception e) {
			Log.error("Error realizando update TLTRTRA", e);
		}
		if("000".equals(codError)){
			getResponseDto().setAssistantCodError(ConstantesFO.COD_ERROR_SUCCESSFUL);
		}
		else {
			getResponseDto().setAssistantCodError(ConstantesFO.COD_ERROR);
		}
	}

	

}